package com.ksd.modules.members.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ksd.common.enums.DeletedEnums;
import com.ksd.common.enums.StatusEnums;
import com.ksd.common.po.BatchIdsPO;
import com.ksd.common.web.threadlocal.SystemIdThrealLocal;
import com.ksd.commons.BaseServiceImpl;
import com.ksd.modules.members.entity.AdminUserBenefits;
import com.ksd.modules.members.mapper.AdminUserBenefitsMapper;
import com.ksd.modules.members.po.AdminUserBenefitsSavePO;
import com.ksd.modules.members.po.AdminUserBenefitsSearchPO;
import com.ksd.modules.members.service.IAdminUserBenefitsService;
import com.ksd.modules.members.vo.AdminUserBenefitsVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class AdminUserBenefitsServiceImpl extends BaseServiceImpl<AdminUserBenefitsMapper, AdminUserBenefits> implements IAdminUserBenefitsService {

    /**
     * 查询所有数据
     */
    public List<AdminUserBenefitsVo> findAllAdminUserBenefits(){
        LambdaQueryWrapper<AdminUserBenefits> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(AdminUserBenefits::getSystemId,SystemIdThrealLocal.get().getSystemId());

        List<AdminUserBenefits> adminUserBenefitsList = this.list(lambdaQueryWrapper);
        return tranferListBo(adminUserBenefitsList, AdminUserBenefitsVo.class);
    }


    /**
     * 查询列表
     * @param adminUserBenefitsSearchPO
     * @return
     */
    public Page<AdminUserBenefitsVo> findAdminUserBenefits(AdminUserBenefitsSearchPO adminUserBenefitsSearchPO){
        Page<AdminUserBenefits> page = new Page<>(adminUserBenefitsSearchPO.getPageNo(),adminUserBenefitsSearchPO.getPageSize());
        LambdaQueryWrapper<AdminUserBenefits> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(AdminUserBenefits::getIsDeleted, DeletedEnums.UN_DELETED.getCode());
        lambdaQueryWrapper.eq(AdminUserBenefits::getSystemId,SystemIdThrealLocal.get().getSystemId());
        // 模糊匹配
        if(StringUtils.isNotEmpty(adminUserBenefitsSearchPO.getKeyword())){
            lambdaQueryWrapper.and(c -> c.like(
                            AdminUserBenefits::getTitle,adminUserBenefitsSearchPO.getKeyword())

            );
        }

        lambdaQueryWrapper.orderByAsc(AdminUserBenefits::getSorted);
        Page<AdminUserBenefits> adminUserBenefitsPage = this.page(page, lambdaQueryWrapper);
        Page<AdminUserBenefitsVo> adminUserBenefitsVoPage = tranferPageBo(adminUserBenefitsPage, AdminUserBenefitsVo.class);
        return adminUserBenefitsVoPage;
    }

    /**
     * 回收站
     * @param adminUserBenefitsSearchPO
     * @return
     */
    public Page<AdminUserBenefitsVo> findAdminUserBenefitsRecyle(AdminUserBenefitsSearchPO adminUserBenefitsSearchPO){
        Page<AdminUserBenefits> page = new Page<>(adminUserBenefitsSearchPO.getPageNo(),adminUserBenefitsSearchPO.getPageSize());
        LambdaQueryWrapper<AdminUserBenefits> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(AdminUserBenefits::getIsDeleted, DeletedEnums.DELETED.getCode());
        lambdaQueryWrapper.eq(AdminUserBenefits::getSystemId,SystemIdThrealLocal.get().getSystemId());
        // 模糊匹配
        if(StringUtils.isNotEmpty(adminUserBenefitsSearchPO.getKeyword())){
            lambdaQueryWrapper.and(c -> c.like(
                    AdminUserBenefits::getTitle,adminUserBenefitsSearchPO.getKeyword())

            );
        }

        lambdaQueryWrapper.orderByAsc(AdminUserBenefits::getSorted);
        Page<AdminUserBenefits> adminUserBenefitsPage = this.page(page, lambdaQueryWrapper);
        Page<AdminUserBenefitsVo> adminUserBenefitsVoPage = tranferPageBo(adminUserBenefitsPage, AdminUserBenefitsVo.class);
        return adminUserBenefitsVoPage;
    }

    /**
     * 保存和更新
     * @param adminUserBenefitsSavePO
     * @return
     */
    @Transactional(rollbackFor=Exception.class)
    public AdminUserBenefitsVo saveUpdateAdminUserBenefits(AdminUserBenefitsSavePO adminUserBenefitsSavePO){
        AdminUserBenefits adminUserBenefits = tranferBo(adminUserBenefitsSavePO, AdminUserBenefits.class);
        adminUserBenefits.setSystemId(SystemIdThrealLocal.get().getSystemId());
        adminUserBenefits.setSystemName(SystemIdThrealLocal.get().getSystemName());
        boolean b = this.saveOrUpdate(adminUserBenefits);
        AdminUserBenefitsVo adminUserBenefitsVo = tranferBo(adminUserBenefits, AdminUserBenefitsVo.class);
        return adminUserBenefitsVo;
    }

    /**
     * 根据ID获取明细
     * @param id
     * @return
     */
    public AdminUserBenefitsVo getAdminUserBenefitsById(Long id){
        AdminUserBenefits adminUserBenefits = this.getById(id);
        AdminUserBenefitsVo adminUserBenefitsVo = tranferBo(adminUserBenefits, AdminUserBenefitsVo.class);
        return adminUserBenefitsVo;
    }

    /**
     * 单个逻辑删除
     * @param id
     * @return
     */
    @Transactional(rollbackFor=Exception.class)
    public Boolean deleteAdminUserBenefitsById(Long id){
        boolean b =  this.updateStatus(id, AdminUserBenefits.Columns.IS_DELETED,1);
        return b;
    }

    /**
     * 单个恢复
     * @param id
     * @return
     */
    @Transactional(rollbackFor=Exception.class)
    public Boolean recoverByIdAdminUserBenefits(Long id){
        boolean b =  this.updateStatus(id, AdminUserBenefits.Columns.IS_DELETED,0);
        return b;
    }

    /**
     * 单个物理删除
     * @return
     */
    @Transactional(rollbackFor=Exception.class)
    public Boolean removeAdminUserBenefitsById(Long id){
        boolean b = this.removeById(id);
        return b;
    }

    /**
     * 批量逻辑删除
     * @param batchIdsPO
     * @return
     */
    @Transactional(rollbackFor=Exception.class)
    public Boolean bacthDeletesAdminUserBenefits(BatchIdsPO batchIdsPO){
        if (StringUtils.isEmpty(batchIdsPO.getIds())) {
            return false;
        }
        // 把文章分类进行恢复
        List<Long> ids = Arrays.stream(batchIdsPO.getIds().split(",")).map(Long::parseLong).collect(Collectors.toList());
        UpdateWrapper<AdminUserBenefits> updateWrapper = new UpdateWrapper<>();
        updateWrapper.set("is_deleted", DeletedEnums.DELETED.getCode());
        updateWrapper.in("id", ids);
        boolean b = this.update(updateWrapper);
        return true;
    }

    /**
     * 批量恢复
     * @param batchIdsPO
     * @return
     */
    @Transactional(rollbackFor=Exception.class)
    public Boolean batchRecoversAdminUserBenefits(BatchIdsPO batchIdsPO){
        if (StringUtils.isEmpty(batchIdsPO.getIds())) {
            return false;
        }
        // 把文章分类进行恢复
        List<Long> ids = Arrays.stream(batchIdsPO.getIds().split(",")).map(Long::parseLong).collect(Collectors.toList());
        UpdateWrapper<AdminUserBenefits> updateWrapper = new UpdateWrapper<>();
        updateWrapper.set("is_deleted", DeletedEnums.UN_DELETED.getCode());
        updateWrapper.in("id", ids);
        boolean b = this.update(updateWrapper);
        return b;
    }

    /**
     * 批量物理删除
     * @param batchIdsPO
     * @return
     */
    @Transactional(rollbackFor=Exception.class)
    public Boolean batchRemovesAdminUserBenefits(BatchIdsPO batchIdsPO){
        if (StringUtils.isEmpty(batchIdsPO.getIds())) {
            return false;
        }
        // 把文章分类进行恢复
        List<Long> ids = Arrays.stream(batchIdsPO.getIds().split(",")).map(Long::parseLong).collect(Collectors.toList());
        boolean b = this.removeByIds(ids);
        return b;
    }


    /**
     * 修改状态
     * @param id
     * @param column
     * @param value
     * @return
     */
    @Override
    @Transactional(rollbackFor=Exception.class)
    public boolean updateStatus(Long id,String column,Integer value){
        boolean b = super.updateStatus(id,column,value);
        return b;
    }

    /**
     * 修改状态
     * @param id
     * @param column
     * @param value
     * @return
     */
    @Override
    public boolean updateStatusStr(Long id,String column,String value){
        boolean b = super.updateStatusStr(id,column,value);
        return b;
    }

    /**
     * 批量上架
     *
     * @param batchIdsPO
     * @return
     */
    @Override
    public Boolean batchDowns(BatchIdsPO batchIdsPO) {
        if (StringUtils.isEmpty(batchIdsPO.getIds())) {
            return false;
        }

        // 把文章进行恢复
        List<Long> ids = Arrays.stream(batchIdsPO.getIds().split(",")).map(Long::parseLong).collect(Collectors.toList());
        UpdateWrapper<AdminUserBenefits> updateWrapper = new UpdateWrapper<>();
        updateWrapper.set("status", StatusEnums.UN_STATUS.getCode());
        updateWrapper.in("id", ids);
        return this.update(updateWrapper);
    }

    /**
     * 批量上架
     *
     * @param batchIdsPO
     * @return
     */
    @Override
    public Boolean batchUps(BatchIdsPO batchIdsPO) {
        if (StringUtils.isEmpty(batchIdsPO.getIds())) {
            return false;
        }

        // 把文章进行恢复
        List<Long> ids = Arrays.stream(batchIdsPO.getIds().split(",")).map(Long::parseLong).collect(Collectors.toList());
        UpdateWrapper<AdminUserBenefits> updateWrapper = new UpdateWrapper<>();
        updateWrapper.set("status", StatusEnums.STATUS.getCode());
        updateWrapper.in("id", ids);
        return this.update(updateWrapper);
    }

}
